package com.ztej.query.parser;

import com.ztej.query.model.Contact;
import com.ztej.query.model.ContactInfo;
import com.ztej.query.model.Region;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mac_xhq on 2017/1/16.
 */

public class JsonHelper {

    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    List<Map<String, Map<String, String>>> list2 = new ArrayList<Map<String, Map<String, String>>>();
    Map<String, String> map = null;
    Map<String, Map<String, String>> map2 = null;

    public Map<String, String> parse(String jsonData){
        try {
            JSONObject jsonObject=new JSONObject(jsonData);
            if (jsonObject.getString("result_code").equals("0000")){
                map = new HashMap<String, String>();
                map.put("result_code", jsonObject.getString("result_code"));
                map.put("result_info", jsonObject.getString("result_info"));
            }else {
                map = new HashMap<String, String>();
                map.put("result_code", jsonObject.getString("result_code"));
                map.put("result_info", jsonObject.getString("result_info"));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return map;
    }



    //解析部门
    List<Region> list_region = new ArrayList<Region>();
    public List<Region> parseRegion(String jsonData){
        try {
            JSONObject jsonObject=new JSONObject(jsonData);
            if (jsonObject.getString("Code").equals("1")){
                JSONArray jsonArray = jsonObject.getJSONArray("Region");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject item = jsonArray.getJSONObject(i); // 得到每个对象
                    Region region = new Region();
                    region.setFid(item.getString("fid"));
                    region.setName(item.getString("name"));
                    region.setParentid(item.getString("parentid"));
                    list_region.add(region);
                }

            }else {
//                map = new HashMap<String, String>();
//                map.put("code", jsonObject.getString("code"));
//                list.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list_region;
    }




    //解析联系人
    List<Contact> list_contact = new ArrayList<Contact>();
    public List<Contact> parseContact(String jsonData){
        try {
            JSONObject jsonObject=new JSONObject(jsonData);
            if (jsonObject.getString("Code").equals("1")){
            JSONArray jsonArray = jsonObject.getJSONArray("Contacts");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject item = jsonArray.getJSONObject(i); // 得到每个对象
                Contact contact = new Contact();
                contact.setId(item.getString("id"));
                contact.setName(item.getString("name"));
                contact.setRegion(item.getString("region"));
                contact.setPhone(item.getString("phone"));
                contact.setPhotourl(item.getString("photourl"));
                list_contact.add(contact);
            }

            }else {
//                map = new HashMap<String, String>();
//                map.put("result_code", jsonObject.getString("result_code"));
//                map.put("result_info", jsonObject.getString("result_info"));
//                list.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list_contact;
    }


    //解析联系人信息
    List<ContactInfo> list_contactInfo = new ArrayList<ContactInfo>();
    public List<ContactInfo> parseContactInfo(String jsonData){
        try {
            JSONObject jsonObject=new JSONObject(jsonData);
            if (jsonObject.getString("Code").equals("1")){
                JSONArray jsonArray = jsonObject.getJSONArray("User");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject item = jsonArray.getJSONObject(i); // 得到每个对象

                    ContactInfo contactInfo = new ContactInfo();
                    contactInfo.setName(item.getString("name"));
                    contactInfo.setRegion(item.getString("region"));
//                    contactInfo.setPhone();
                    contactInfo.setPhotourl(item.getString("photourl"));
                    contactInfo.setSex(item.getString("sex"));
                    contactInfo.setRole(item.getString("role"));
                    contactInfo.setAge(item.getString("age"));
                    contactInfo.setDuty(item.getString("duty"));
                    list_contactInfo.add(contactInfo);

                }

            }else {
//                map = new HashMap<String, String>();
//                map.put("result_code", jsonObject.getString("result_code"));
//                map.put("result_info", jsonObject.getString("result_info"));
//                list.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list_contactInfo;
    }




}
